/*
       1. 数组求和及平均值
       let arr1 = [7, 2, 1, 17, 23];
      */
let arr1 = [7, 2, 1, 17, 23];
const sum1 = arr1.reduce((total, value) => total + value);
console.log('sum1', sum1);
const res1 = arr1.reduce((total, value) => total + value) / arr1.length;
console.log('res1', res1);

/*
        2. 求数组[12,52,12,7,123,5,17]中的最大值。
      */
let arr2 = [12, 52, 12, 7, 123, 5, 17];
const res2 = arr2.sort((a, b) => b - a);
console.log('res2', res2[0]);

/*
        3. 将数组 ['red', 'green', 'blue', 'pink'] 里面的元素转换为字符串, 输出“redgreenbluepink”
      */
let arr3 = ['red', 'green', 'blue', 'pink'];
const res3 = arr3.join('');
console.log('res3', res3);

/*
        4. 将数组 ['red', 'green', 'blue', 'pink'] 转换为字符串，并且用 | 或其他符号分割, 输出: 'red|green|blue|pink'
      */
let arr4 = ['red', 'green', 'blue', 'pink'];
// const res4 = arr4.join("|");
// console.log("res4", res4);

function fn4(arr) {
  let str = '';
  arr.forEach((v, i, array) => {
    if (i === array.length - 1) {
      str += v;
    } else str += v + '|';
  });
  return str;
}
console.log('res4', fn4(arr4));

/*
        5. 把数组[2,5,6,7,8]中的每一位数字都增加30%，并返回一个新数组
      */
let arr5 = [2, 5, 6, 7, 8];
// arr5.forEach((item) => {
//   res5.push(item * 1.3);
// });
const res5 = arr5.map((item) => item * 1.3);
console.log('res5', res5);

/*
        6. 编写函数has(arr, 20) 判断数组中是否存在20这个元素，返回布尔类型
      */
arr = [1, 2, 34, 42, 20];
function has(arr, tag) {
  console.log(arr.includes(tag));
}
has(arr, 20);

/*
        7. 根据数组中对比age的年龄, 从大到小排序
        let arr7 = [
      {age: 19, name:'a'},
      {age: 50, name:'b'},
      {age: 12, name:'c'},
      {age: 25, name:'d'}
        ]
      */
let arr7 = [
  { age: 19, name: 'a' },
  { age: 50, name: 'b' },
  { age: 12, name: 'c' },
  { age: 25, name: 'd' },
];
const res7 = arr7.sort((a, b) => b.age - a.age);
console.log('res7', res7);

/*
        8. 改变传入的数组，将数组中第 n(从 0 开始算 ) 个元素放到数组的开头
      */
let arr8 = [2, 3, 1, 4, 5, 6];
function fn8(arr, n) {
  const nextArr = arr.splice(n, 1)[0];
  arr.unshift(nextArr);
  return arr;
}
console.log('fn8', fn8(arr8, 2));

/*
        9. 将函数中的arguments 对象转换成真正的数组
      */
function fn9(arr) {
  const res9 = Array.from(arguments);
  console.log('res9', res9);
}

/*
        10. 如何实现一个数组push方法
      */
let arr10 = [1, 2, 3, 4, 5];
function push(arr, ...values) {
  for (const value of values) {
    arr[arr.length] = value;
  }
  return arr;
}
console.log('push', push(arr10, 6, 7, 8, 9));

/*
        11. 数组去重的方法 (4种)
      */
let array = [1, 2, 2, 3, 4, 4, 5];

// Set方法
let arr11 = Array.from(new Set(array));
console.log(arr11); // 输出: [1, 2, 3, 4, 5]

//indexOf方法
const arr12 = [];
array.forEach((item, index, arr) => {
  if (arr12.indexOf(array[index]) === -1) {
    arr12.push(item);
  }
});
console.log('arr12', arr12); // 输出: [1, 2, 3, 4, 5]

//includes方法
const arrr3 = [];
array.forEach((index) => {
  if (!arrr3.includes(array[index])) {
    arrr3.push(array[index]);
  }
});
console.log('arr3', arrr3); // 输出: [1, 2, 3, 4, 5]

//reduce方法
const arrr4 = array.reduce((arr, current) => {
  if (arr.indexOf(current) === -1) {
    arr.push(current);
  }
  return arr;
}, []);
console.log(arrr4); // 输出: [1, 2, 3, 4, 5]
